<template>
<div class="register">
<main class="register-main">
  <div class="nav">
    <h1>欢迎您注册</h1>
  </div>
  <div class="register-form">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
      <el-form-item label="请输入用户名" prop="username">
        <el-input type="string" v-model="ruleForm.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="请输入密码" prop="password">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkpass">
        <el-input v-model="ruleForm.checkpass"></el-input>
      </el-form-item>
      <el-form-item label="请输入手机号" prop="phone">
        <el-input v-model.number="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="请输入验证密钥" prop="token">
        <el-input v-model="ruleForm.token"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</main>
</div>
</template>

<script>
import { ajax } from '@/config/user'

export default {
name: "register",
  data(){
    var checkusername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('用户名不能为空'));
      }else {
        callback();
      }
    };
    var checkpassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('密码不能为空'));
      }else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    var checkphone= (rule, value, callback) => {
      if (!value) {
        callback(new Error('手机号码不能为空'));
      }
      if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(value))){
        callback(new Error('请输入正确的手机号'));
      }
      else {
        callback();
      }
    };
   return{
     ruleForm: {
       username: '',
       password: '',
       phone: '',
       token:'',
       checkpass:''
     },
     rules: {
       username: [
         { validator: checkusername, trigger: 'blur' ,required:true}
       ],
       password: [
         { validator: checkpassword, trigger: 'blur',required:true }
       ],
       checkpass: [
         { validator: validatePass2, trigger: 'blur',required:true }
       ],
       phone: [
         { validator: checkphone, trigger: 'blur',required:true }
       ],

     }
   }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          ajax.getregister(this.ruleForm).then(res=>{
            console.log(res)
          })
        } else {
          this.$message.error('请检查注册信息是否输入正确')
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped lang="scss">
.register{
  background: url("@/assets/login/bg.jpg");
  background-size: 100% 100%;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  .register-main{
    width: 500px;
    height: 600px;
    margin: auto;
    display: flex;
    background-color: #89b1ed;
    border: 1px solid #54a8fe;
    border-radius: 30px;
    box-shadow: 0 0 3px 2px #54a8fe;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    .nav{
      text-align: center;
     h1{
        font-size: 22px;
     }

    }
    .register-form{
      width: 80%;
    }
  }
}
</style>
